//
//  WTEmoticonPageViewCell.swift
//  WTSinaWeibo
//
//  Created by  Wellim on 16/7/5.
//  Copyright © 2016年  Wellim. All rights reserved.
//

import UIKit

class WTEmoticonPageViewCell: UICollectionViewCell {
    
    var emoticons: [WTEmoticon]?{
    
        didSet{
            // 当每次外界给其赋值的时候 要吧当前cell中的20个按钮全部隐藏（由于cell复用问题）
            // 遍历按钮隐藏
            for button in emoticonButtons {
                
                button.hidden = true
            }
            // 遍历外界传入的一维数组
            for (i,emoticon) in emoticons!.enumerate() {
                
                let button = emoticonButtons[i]
                // 显示button
                button.hidden = false
                
                // 给其button设置emoticon数据
                button.emoticon = emoticon
                /*
                 - 如果type ==  0 代表是图片
                 - 如果type == 1 代表是字符串
                 */
                //代表是emoji表情
                if emoticon.isEmoji {
                    let tit = ((emoticon.code ?? "") as NSString).emoji()
                    
                    button.setTitle(tit, forState: UIControlState.Normal)
                    
                    // 考虑到cell重用问题
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                }else{
                  let path = emoticon.path ?? ""
                  let png = emoticon.png ?? ""
                  let name = path + png
//                  print(name)
                   
                 let image = UIImage(named: name, inBundle:  WTEmoticonTools.sharedEmoticonTools.emotionBundle, compatibleWithTraitCollection: nil)
                    // 代表图片表情
                 button.setImage(image, forState: UIControlState.Normal)
                 // 考虑到cell重用问题
                 button.setTitle(nil, forState: UIControlState.Normal)
                    
               }
                
            }
        
        }
    
    }
    // 保存20个表情按钮
    var emoticonButtons:[WTEmoticonButton] = [WTEmoticonButton]()
    
    
    
    var indexpath: NSIndexPath?{
    
        didSet{
            
//            messagelabel.text = "第\(indexpath?.section ?? 0)组 第\(indexpath?.item ?? 0)页"
        }
    
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    // MARK: - 设置视图
    private  func setUpUI(){
    
//    backgroundColor = RandomColor()
        //添加20个按钮
        addChildButtons()
        
        contentView.addSubview(deleteButton)
//     //添加控件
//     addSubview(messagelabel)
//     
//    //设置约束
//     messagelabel.snp_makeConstraints { (make) in
//        
//      make.center.equalTo(self)
//      }
        
    
    }
    
   
    //设置按钮的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        // 记得按钮的宽度高度
        let w = (kScreenWidth - 10) / CGFloat(EmoticonMaxCol)
        let h = (216 - 37 - 20) / CGFloat(EmoticonMaxRow)
        
        // 遍历emoticonButtons
        for (i,button) in emoticonButtons.enumerate() {
            
            let x = i % EmoticonMaxCol
            let y = i / EmoticonMaxCol
            
            // 设置buttonframe
            button.frame = CGRect(x: CGFloat(x) * w + 5, y: CGFloat(y) * h, width: w, height: h)
            
            
        }
        //设置删除按钮frme
        deleteButton.frame = CGRect(x: kScreenWidth - 5 - w, y: h * 2, width: w, height: h)
        
        
        
    }
    
    //// 创建20个按钮
    func addChildButtons() {
        
        for _ in 0..<EmoticonMaxCount {
            // 创建button
            let button = WTEmoticonButton()
            //字体的大小
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
             // 设置随机颜色
            button.backgroundColor = self.backgroundColor
              //添加点击事件
            button.addTarget(self, action:"buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            // 添加到数组中
            emoticonButtons.append(button)
             // 添加button
            contentView.addSubview(button)
            
        }
        
        
    }
    
    
    // MARK: - 懒加载控件
    private lazy var messagelabel:UILabel = {
     
     let label = UILabel(textColor: UIColor.redColor(), textFont: 35)
      
     label.textAlignment = .Center
       
      return label
    }()
    
    // 删除按钮
    private lazy var deleteButton: UIButton = {
     
        let button = UIButton()
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        //添加点击事件
        button.addTarget(self, action: "deleteBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
      
        return button
    
    }()
    
    // MARK: - 监听事件
    
    @objc private func deleteBtnClick(){
    
     NSNotificationCenter.defaultCenter().postNotificationName(EmoticonDeleteNoti, object: nil)
    
    
    }
   
    // MARK: - 表情按钮监听事件
    @objc private func buttonClick(button: WTEmoticonButton){
    
      NSNotificationCenter.defaultCenter().postNotificationName(EmoticonButtonNoti, object: button.emoticon)
    
    
    }
    
    
}
